package com.qutan.app.core.manager;

import com.alibaba.dubbo.config.annotation.Service;
import com.i7time.service.DynamicService;
import com.qutan.app.core.cache.UserCache;
import com.qutan.app.core.domain.Dynamic;
import com.qutan.app.core.domain.UserInfo;
import com.qutan.app.core.repository.DynamicRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pengweiyuan on 6/27/16.
 */
@Service(version = "1.0.0", interfaceClass = DynamicService.class)
public class DynamicManager implements DynamicService {

    private static Logger logger = org.slf4j.LoggerFactory.getLogger(DynamicManager.class);

    @Autowired
    private DynamicRepository dynamicRepository;


    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private UserCache userCache;


    @Override
    public List<String> getRecentPhotos(String userId) {
        try {
            Map<String, Object> paramaterMap = new HashMap<String, Object>();
            paramaterMap.put("userId", userId);
            List<String> strings = jdbcTemplate.queryForList("select images from dynamic where user_id=:userId and images is not null order by id desc limit 4 ", paramaterMap, String.class);
            List<String> result = new ArrayList<>(4);
            for (String url : strings) {
                String[] urlArr = url.split(";");
                for(String s:urlArr){
                    if(StringUtils.isBlank(s))continue;
                    result.add(s);
                }
                if(result.size()>=4){
                    break;
                }
            }
            return result;
        } catch (Exception e) {
            logger.error("getRecentPhotos error userid={}", userId, e);
            return new ArrayList<>();
        }
    }

    @Override
    public boolean addDynamic(String userId, String content, String[] imagesUrl, String locations, String latitude, String longtitude) {
        try {
            long uId = Long.parseLong(userId);
            UserInfo userInfo = userCache.getUserInfoCheckDbById(uId);
            if (userInfo == null) {
                logger.warn("userInfo==null id={}", userId);
                return false;
            }
            StringBuilder sb = new StringBuilder();
            for (String img : imagesUrl) {
                sb.append(img).append(";");
            }
            long la = Long.parseLong(latitude);
            long lo = Long.parseLong(longtitude);
            Dynamic dynamic = new Dynamic(uId, userInfo.getNick_name(), content, sb.toString(), "", locations, la, lo);
            dynamicRepository.save(dynamic);
            return true;
        } catch (Exception e) {
            logger.error("addDynamic error userid={}", userId, e);
            return false;
        }
    }

    @Override
    public boolean deleteDynamic(String dynamicId) {
        try {
            long dId = Long.parseLong(dynamicId);
            dynamicRepository.delete(dId);
            return true;
        } catch (Exception e) {
            logger.error("deleteDynamic error dynamicId={}", dynamicId, e);
            return false;
        }
    }
}
